package com.vr.xuecheng.content.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vr.xuecheng.content.mapper.CourseMarketMapper;
import com.vr.xuecheng.content.model.dto.CourseAddOrUpdateParamsDTO;
import com.vr.xuecheng.content.model.entity.CourseBase;
import com.vr.xuecheng.content.model.entity.CourseMarket;
import com.vr.xuecheng.content.service.ICourseMarketService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程营销信息 服务实现类
 * </p>
 *
 * @author hsh
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CourseMarketServiceImpl extends ServiceImpl<CourseMarketMapper, CourseMarket> implements ICourseMarketService {
    private final CourseMarketMapper courseMarketMapper;

    @Override
    public CourseMarket saveCourseMarket(CourseAddOrUpdateParamsDTO courseAddOrUpdateParamsDTO, CourseBase courseBase) {
        CourseMarket newCourseMarket = BeanUtil.toBean(courseAddOrUpdateParamsDTO, CourseMarket.class);
        // 查询关联的营销信息是否存在
        CourseMarket queryResult = courseMarketMapper.selectOne(new QueryWrapper<CourseMarket>().lambda()
                .eq(CourseMarket::getId, courseBase.getId()));
        if (queryResult==null) {
            courseMarketMapper.insert(newCourseMarket);
        } else {
            // 存在时,合并新数据,并更新营销信息
            BeanUtil.copyProperties(newCourseMarket, queryResult);
            queryResult.setId(courseBase.getId());
            courseMarketMapper.updateById(queryResult);
        }
        return queryResult;
    }
}
